package cn.imu.appdevexperiment2020.Application.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import cn.imu.appdevexperiment2020.Application.activity.jiemian.ContextFragment1;
import cn.imu.appdevexperiment2020.Application.activity.jiemian.ContextFragment2;
import cn.imu.appdevexperiment2020.Application.activity.jiemian.ContextFragment3;
import cn.imu.appdevexperiment2020.Application.activity.jiemian.ContextFragment4;
import cn.imu.appdevexperiment2020.R;

public class KeyActivity extends AppCompatActivity {
    Fragment f1=new ContextFragment1();
    Fragment f2=new ContextFragment2();
    Fragment f3=new ContextFragment3();
    Fragment f4=new ContextFragment4();
    Button button1,button2,button3,button4;
    public List<Fragment> contextFragments = new ArrayList<>();// 用来存放Fragments的集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key);
        button1= findViewById(R.id.chat);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(f1);
                v.setBackgroundColor(Color.GRAY);
                button2.setBackgroundColor(Color.WHITE);
                button3.setBackgroundColor(Color.WHITE);
                button4.setBackgroundColor(Color.WHITE);
            }
        });
        button2= findViewById(R.id.file);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(f2);
                v.setBackgroundColor(Color.GRAY);
                button1.setBackgroundColor(Color.WHITE);
                button3.setBackgroundColor(Color.WHITE);
                button4.setBackgroundColor(Color.WHITE);
            }
        });
        button3= findViewById(R.id.classes);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(f3);
                v.setBackgroundColor(Color.GRAY);
                button2.setBackgroundColor(Color.WHITE);
                button1.setBackgroundColor(Color.WHITE);
                button4.setBackgroundColor(Color.WHITE);
            }
        });
        button4= findViewById(R.id.user);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeFragment(f4);
                v.setBackgroundColor(Color.GRAY);
                button2.setBackgroundColor(Color.WHITE);
                button3.setBackgroundColor(Color.WHITE);
                button1.setBackgroundColor(Color.WHITE);
            }
        });
        contextFragments.add(f1);
        contextFragments.add(f2);
        contextFragments.add(f3);
        contextFragments.add(f4);
        initFragment();
    }
    public void initFragment()
    {
        changeFragment(f1);
        button1.setBackgroundColor(Color.GRAY);
    }
    public void changeFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for(Fragment f:contextFragments)
        {
            if(fragment==f) {
                transaction.replace(R.id.contextFrameLayout, f);
            }
        }
        transaction.commit();
    }
}